#coding:utf8
import cherrypy
import datetime
from ConnectMSSQL import ms

from head import print_head
from end import print_end

from utf8code import *


_js = """
        <script type="text/javascript">
            var bggs;
            var bgbm;
            $(function()
            {
                $("#exitbut").click(function()
                {
                    exit();
                });
                
                $("#bggs").change(function()
                {
                    bggs=$("#bggs option:selected").text();
                    window.location.href="/sbbgxz/changeGS?bggs=" + bggs;
                });
                
                $("#bgbm").change(function()
                {
                    bgbm=$("#bgbm option:selected").text();
                    window.location.href="/sbbgxz/changeBM?bgbm=" + bgbm;
                });
                
                $("#bgr").change(function()
                {
                    var bggs = $("#bggs option:selected").text();
                    var bgbm = $("#bgbm option:selected").text();
                    var bgr = $("#bgr option:selected").text();
                    var url = "/sbbgxz/check?bggs="+bggs+"&bgbm="+bgbm+"&bgr="+bgr;
                    var xmlhttp;
                    if (window.XMLHttpRequest)
                    {// code for IE7+, Firefox, Chrome, Opera, Safari
                      xmlhttp=new XMLHttpRequest();
                    }
                    else
                    {// code for IE6, IE5
                      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                
                    xmlhttp.onreadystatechange=function()
                    {
                      if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                            if(xmlhttp.responseText)
                            {
                                alert(xmlhttp.responseText);
                            }
                        }
                    }
                    xmlhttp.open("GET",url,true);
                    xmlhttp.send();
                });
            });
        </script>
        """
        
_top = """
        <div class="class_sbbh_div">设备编号:<font>%s</font>,原用人:<font>%s</font></div>
        <form method="get" action="addBG">
        <div id="div_operate">
            <table width="auto">
                <tr>
                  <td class="class_td">变更公司<font>*</font></td>
                  <td>
                        <select id="bggs" name="bggs">
                            <option></option>
                            %s
                        </select>
                  </td>
                  <td class="class_td">变更部门<font>*</font></td>
                  <td>
                        <select id="bgbm" name="bgbm">
                        <option></option>
                        %s
                        </select>
                  </td>
                  <td class="class_td">变更人<font>*</font></td>
                  <td>
                        <select id="bgr" name="bgr">
                            <option></option>
                            %s
                        </select>
                  </td>
                </tr>
                <tr>
                  <td class="class_td" colspan="4">变更备注</td>
                  <td class="class_td">经手人<font>*</font></td>
                  <td><input type="text" name="jsr" /></td>
                </tr>
                <tr>
                  <td colspan="6"><textarea name="bgbz"></textarea></td>
                </tr>
            </table>
        </div>
        <div align="right">
            <input type="submit" id="savebut" value="保 存" />
            <input type="button" id="exitbut" value="取 消" />
            <input type="reset" id="resetbut" value="重置" />
        </div>
        """
           
_end = """
        </form>
        """



class BGXZ:
    sbbh = ''
    yyr = ''
    bggs = ''
    @cherrypy.expose
    def index(self,sbbh):#设备变更
        self.sbbh = sbbh
        sql_yyr = """
            SELECT GS,BM,RY 
            FROM T_BMRY 
            WHERE ID = (
                SELECT RYID 
                FROM T_SBBG 
                WHERE BGDH = (
                    SELECT MAX(BGDH) AS MAX_BGDH 
                    FROM T_SBBG
                    WHERE SBBH = '%s'
                )
            )
        """
        dataList_yyr = ms.ExecQuery(sql_yyr % sbbh)
        if dataList_yyr:
            self.yyr = '【' + dataList_yyr[0][0] + '】【' + dataList_yyr[0][1] + '】【' + dataList_yyr[0][2] + '】'
        
        _mid_init_bggs = """
                <option>%s</option>
                """
                
        sql_bggs = """
            SELECT GS FROM T_BMRY GROUP BY GS;
            """
        
        dataList_bggs = ms.ExecQuery(sql_bggs)

        _mid_bggs = ''
        _mid_bgbm = ''
        _mid_bgr = ''
        for bggs in dataList_bggs:
            _mid_bggs +=_mid_init_bggs % (bggs)
        
        return utf8code(print_head % ('变更','新增设备变更') + _js + _top % (self.sbbh,self.yyr,_mid_bggs,'','') + _end + print_end)
    
    #####################################################################
    #公司下拉框选择后针对该公司下的部门显示到部门下拉框中
    #####################################################################
    @cherrypy.expose
    def changeGS(self,bggs):
        self.bggs = bggs
        _mid_init_bggs = """
                <option>%s</option>
                """
        _mid_init_bgbm = """
                <option>%s</option>
                """
                
        sql_bggs = """
            SELECT GS FROM T_BMRY GROUP BY GS;
            """
        sql_bgbm = """
            SELECT BM FROM T_BMRY WHERE GS = '%s' GROUP BY BM;
            """
        
        dataList_bggs = ms.ExecQuery(sql_bggs)
        dataList_bgbm = ms.ExecQuery(sql_bgbm % bggs)

        _mid_bggs = ''
        _mid_bgbm = ''
        for gs in dataList_bggs:
            #元组的第一个元素进行解码并与所点选的部门匹配
            if gs[0] != bggs:
                _mid_bggs += _mid_init_bggs % (gs)
            else:
                _mid_bggs += '<option selected="selected">%s</option>' % (gs)
        for bm in dataList_bgbm:
            _mid_bgbm += _mid_init_bgbm % (bm)
        
        return utf8code(print_head % ('变更','新增设备变更') + _js + _top % (self.sbbh,self.yyr,_mid_bggs,_mid_bgbm,'') + _end + print_end)

    
    #####################################################################
    #部门下拉框选择后针对该部门下的人员显示到人员下拉框中
    #####################################################################
    @cherrypy.expose
    def changeBM(self,bgbm):
        _mid_init_bggs = """
                <option>%s</option>
                """
        _mid_init_bgbm = """
                <option>%s</option>
                """
        _mid_init_bgr = """
                <option>%s</option>
                """
                
        sql_bggs = """
            SELECT GS FROM T_BMRY GROUP BY GS;
            """
        sql_bgbm = """
            SELECT BM FROM T_BMRY WHERE GS = '%s' GROUP BY BM;
            """
        sql_bgr = """
            SELECT RY FROM T_BMRY  WHERE GS = '%s' AND BM = '%s' ORDER BY RY
            """
        
        dataList_bggs = ms.ExecQuery(sql_bggs)
        dataList_bgbm = ms.ExecQuery(sql_bgbm % self.bggs)
        dataList_bgr = ms.ExecQuery(sql_bgr % (self.bggs,bgbm))

        _mid_bggs = ''
        _mid_bgbm = ''
        _mid_bgr = ''
        for gs in dataList_bggs:
            #元组的第一个元素进行解码并与所点选的部门匹配
            if gs[0] != self.bggs:
                _mid_bggs += _mid_init_bggs % (gs)
            else:
                _mid_bggs += '<option selected="selected">%s</option>' % (gs)
        for bm in dataList_bgbm:
            #元组的第一个元素进行解码并与所点选的部门匹配
            if bm[0] != bgbm:
                _mid_bgbm += _mid_init_bgbm % (bm)
            else:
                _mid_bgbm += '<option selected="selected">%s</option>' % (bm)
        
        for bgr in dataList_bgr:
            _mid_bgr += _mid_init_bgr % (bgr)
        
        return utf8code(print_head % ('变更','新增设备变更') + _js + _top % (self.sbbh,self.yyr,_mid_bggs,_mid_bgbm,_mid_bgr) + _end + print_end)


    @cherrypy.expose
    def addBG(self,bggs,bgbm,bgr,jsr,bgbz):
        if len(bgr) > 0 and len(jsr) > 0:
            sql = "EXECUTE PRC_ADD_BG '%s','%s','%s','%s','%s','%s';" % (self.sbbh,bggs,bgbm,bgr,jsr,bgbz)
            ms.ExecNonQuery(sql)
            return print_head % ("操作提示","保存成功!*^_^*") + "<body onLoad=" + """javascript:setTimeout("exit()",3000);""" + ">正在关闭此页,请稍等......</body>" + print_end
        else:
            return print_head % ("操作提示","保存失败!-_-||") + """<body onLoad='javascript:setTimeout("exit()",3000);'>带红色<font>*</font>号的为必填项,正在关闭此页,请稍等......</body>""" + print_end
                    
    @cherrypy.expose
    def check(self,bggs,bgbm,bgr):
        msg = ''
        sql = """
            SELECT A.RYID,COUNT(B.SBBH) FROM T_SBBG A
            LEFT JOIN (SELECT SBBH,MAX(BGDH) AS MAX_BGDH FROM T_SBBG GROUP BY SBBH) B ON A.BGDH = B.MAX_BGDH
            WHERE A.RYID = (SELECT ID FROM T_BMRY WHERE GS = '%s' AND BM = '%s' AND RY = '%s')
            GROUP BY A.RYID
        """#这个人手里现有的设备数量
        dataList = ms.ExecQuery(sql % (bggs,bgbm,bgr))
        if dataList[0][0]:
            if dataList[0][1] > 0:#领用第2台即提示
                msg += '1、%s手里已有' % bgr + str(dataList[0][1]) + '台设备!原设备勿忘归还!'
        
        sql_jccf = """
            SELECT COUNT(SBBH) FROM T_SBBG 
            WHERE RYID = (SELECT ID FROM T_BMRY WHERE RY = '%s') 
                AND SBBH = '%s'
        """#查看该变更人手头上的设备是否重复变更
        dataList_jccf = ms.ExecQuery(sql_jccf % (bgr,self.sbbh))
        if dataList_jccf[0][0]:#此人重复领用将提示操作者
            msg += '\n2、该设备目前就在%s手里，请勿重复变更!' % bgr
            
        return utf8code(msg)
    
    @cherrypy.expose
    def sbgh(self,sbbh):
        self.sbbh = sbbh
        gh_gs = '<option selected="selected">%s</option>' % '聚隆'
        gh_bm = '<option selected="selected">%s</option>' % '信息优化提示部'
        gh_ghr = '<option selected="selected">%s</option>' % '闲置'
        return utf8code(print_head % ('归还','新增设备归还') + _js + _top % (self.sbbh,self.yyr,gh_gs,gh_bm,gh_ghr) + _end + print_end)

    @cherrypy.expose
    def sbbf(self,sbbh):
        self.sbbh = sbbh
        gh_gs = '<option selected="selected">%s</option>' % '聚隆'
        gh_bm = '<option selected="selected">%s</option>' % '信息优化提示部'
        gh_ghr = '<option selected="selected">%s</option>' % '报废'
        return utf8code(print_head % ('报废','新增设备报废') + _js + _top % (self.sbbh,self.yyr,gh_gs,gh_bm,gh_ghr) + _end + print_end)
